mod spotify;
use tracing::{info, warn, Level, debug};
use tracing_subscriber::FmtSubscriber;
// use rspotify::clients::BaseClient;
use teloxide::{prelude::*, adaptors::throttle::Limits, utils::command::BotCommands};
use anyhow::{Result, Context};

#[tokio::main]
async fn main() {
    run_bot().await;
}
async fn run_bot() {
    let subscriber = FmtSubscriber::builder()
    .with_max_level(Level::INFO)
    .finish();
 tracing::subscriber::set_global_default(subscriber)
    .expect("setting default subscriber failed");
let config = Config::load_config().await;
let bot = teloxide::Bot::new(&config.bot_token)
// .parse_mode(ParseMode::MarkdownV2)
.throttle(Limits::default()); //.unwrap();

let handler = dptree::entry()
.branch(Update::filter_message().endpoint(messages_handler));
Dispatcher::builder(bot, handler)
.enable_ctrlc_handler()
.distribution_function(|_| None::<std::convert::Infallible>)
.build()
.dispatch()
.await;

}

async fn messages_handler(m: Message, bot: Bot){
    if let Some(text) = parse_text(&m){
        if let Ok(command) = BotCommands::parse(&text, "readlaterbot") {
            command_answer(bot, m, command).await;
 
    }
}
}


fn parse_text<'a>(m: &'a Message) -> Option<&'a str> {
    let mut res = None;
    if let Some(text) = m.text() {
        res = Some(text);
    }
    if let Some(text) = m.caption() {
        res = Some(text);
    }
    res
}

async fn command_answer(bot: Bot, m: Message, command: Command){
    match command {
        Command::Start => {},
        Command::Search(query) => search(query).await.unwrap()
    }
}
// use rspotify::clients::BaseClient;
// async fn search(query: String)-> Result<()>{
//     // BaseClient::search(&'life0 self, q, type, market, include_external, limit, offset)
//     // let client = rspotify::ClientCredsSpotify::
// }
#[derive(Debug, BotCommands)]
enum Command{
    Start,
    Search(String)
}

use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct Config{
    bot_token: String,
}
impl Config {
    async fn load_config() -> Self{
        unimplemented!()
    } 
}